package com.igeek.collateral.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Collateral;
import com.igeek.collateral.entity.Inbound;
import com.igeek.collateral.entity.Role;
import com.igeek.collateral.entity.UserRole;
import com.igeek.collateral.mapper.InboundMapper;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IInboundService;
import com.igeek.collateral.vo.InboundCollateralVO;
import com.igeek.collateral.vo.PageQueryVO;
import com.igeek.collateral.vo.ZInboundVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Stream;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/5
 **/

@RestController
@RequestMapping("/admin/inbound")
@Api(tags = "入库接口")
@Slf4j
public class InBoundController {
    @Autowired
    private IInboundService iInboundService;



    //查全部已进库
    @GetMapping("findPageIn")
    @ApiOperation("条件分页查询进库信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Inbound> page = iInboundService.findPageIn(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }



    @GetMapping("findPagAll")
    @ApiOperation("条件分页查询进库信息")
    public Result findPageAll(PageQueryVO vo){
        try {
            Page<Inbound> page = iInboundService.findPageAll(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //查询正常入库的信息条件
    @GetMapping("findPageZ")
    @ApiOperation("条件分页查询正常进库信息")
    public Result findPageZ(PageQueryVO vo){
        try {
            Page<Inbound> page = iInboundService.findPageZ(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //查询置换入库的信息条件
    @GetMapping("findPageR")
    @ApiOperation("条件分页查询置换进库信息")
    public Result findPageR(PageQueryVO vo){
        try {
            Page<Inbound> page = iInboundService.findPageR(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }




    //通过id查询进库信息
    @GetMapping("/{id}")
    @ApiOperation("通过ID查询入库信息")
    public Result findById(@PathVariable("id") Integer id){
        Inbound inbound = iInboundService.getById(id);
        if(inbound!=null) {
            return new Result(true, "查询信息成功", inbound);
        }
        return new Result(false , "查询信息失败");
    }


    //通过id更新对应的信息
    @PutMapping("update/collateralId")
    @ApiOperation("通过ID修改入库信息")
    public Result update(@RequestBody Inbound inbound ,@RequestParam Integer collateralId)
    {
        boolean b = iInboundService.update(inbound,collateralId);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");

    }

    //
    @PutMapping("update/status")
    @ApiOperation("修改状态信息")
    public Result updateS(@RequestBody Inbound inbound )
    {
        boolean b = iInboundService.updateById(inbound);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");

    }


    //新增入库记录
    @PostMapping("add")
    @ApiOperation("添加入库记录")
    public Result add(@RequestBody Inbound inbound,@RequestParam("collateralId") Integer collateralId)
    {
        try {
            boolean save = iInboundService.add(inbound,collateralId);
            if(save)
            {
                return new Result(true,"添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加失败");

    }


    //删除操作，根据进库id删除
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除进库信息")
    public Result delete (@PathVariable("id") Long id)
    {
        try {
            boolean flag=iInboundService.delete(id);
            if(flag)
            {
                return new Result(true,"删除成功");
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }



    //通过id更新对应的信息（置换入库）(点击确定后的实现)
    @PutMapping("updateS/RcollateralId")
    @ApiOperation("通过replaceId修改置换入库信息")
    public Result updateRS(@RequestBody Inbound inbound ,@RequestParam("collateralId") Integer collateralId)
    {
        //返回入库的总记录数
        boolean b = iInboundService.updateRS(inbound,collateralId);
        if(b)
        {
            return new Result(true,"更新成功",b);
        }
        return new Result(false,"更新失败");
    }





    //通过id修改该入库的状态为未入库(否决的)
    @PutMapping("updateReplaceS")
    @ApiOperation("将入库状态修改为未入库")
    public Result updateById(@RequestBody Inbound inbound ) {
        boolean b=   iInboundService.updateById(inbound);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");
    }


    //正常进库修改部分信息
    @PutMapping("updatePart")
    @ApiOperation("修改进库信息的部分信息")
    public Result updatePart(@RequestBody ZInboundVo vo,@RequestParam("inboundId") Integer inboundId)
    {
        boolean b=iInboundService.updatePart(vo,inboundId);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");
    }


    //通过押品id找到对应的进库类型
    @GetMapping("check")
    @ApiOperation("查找对应的进库类型")
    public Result checkType(@RequestParam("collateralId") Integer collateralId)
    {
        int a=iInboundService.check(collateralId);
        if(!ObjectUtils.isEmpty(a)){
            return new Result(true,"查找成功",a);
        }
        return new Result(false,"查找失败");
    }



    //根据押品id删除进库记录
    @DeleteMapping("deleteByCid")
    @ApiOperation("根据押品id删除进库信息")
    public Result deleteMessByCid(@RequestParam("collateralId") Integer collId)
    {
        boolean b=iInboundService.deleteMessByCid(collId);
        if(b)
        {
            return new Result(true,"删除对应信息成功");
        }
        return new Result(false,"删除对应信息失败");
    }











}
